﻿namespace ManagerClinic.CauHinh
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Ultilities.Ultility;
    using System.Linq;
    using ManagerClinic.Modules;

    public partial class frmCauHinh : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler cauhinhEvent;

        CauHinhEntity cauHinhEntity = null;
        CauHinhBusiness cauHinhBusiness = null;

        IList<CauHinhChiTietEntity> lstCauHinhChiTietEntity = null;
        IList<CauHinhChiTietEntity> lstGridCauHinhChiTietEntity = new List<CauHinhChiTietEntity>();
        CauHinhChiTietBusiness cauHinhChiTietBusiness = null;

        DmPhongKhamBusiness dmPhongKhamBusiness = null;
        IList<DmPhongKhamEntity> lstDmPhongKhamEntity = null;

        int State = 0;
        Int64 MaCauHinh = 0;

        public frmCauHinh(int state)
        {
            InitializeComponent();

            cauHinhBusiness = new CauHinhBusiness();
            cauHinhChiTietBusiness = new CauHinhChiTietBusiness();
            dmPhongKhamBusiness = new DmPhongKhamBusiness();

            this.State = state;
            this.MaCauHinh = 0;

            this.LoadDmNguoiDung();
            this.LoadData();
        }

        public frmCauHinh(int state, CauHinhEntity obj)
        {
            InitializeComponent();

            this.State = state;
            this.cauHinhEntity = obj;
            this.MaCauHinh = obj.MaCauHinh;

            cauHinhBusiness = new CauHinhBusiness();
            cauHinhChiTietBusiness = new CauHinhChiTietBusiness();
            dmPhongKhamBusiness = new DmPhongKhamBusiness();

            this.LoadDmNguoiDung();
            this.SetData();
            this.LoadData();

            this.lkNguoiDung.Enabled = false;
        }

        void SetData()
        {
            lkNguoiDung.EditValue = this.cauHinhEntity.MaNguoiDung;
        }

        void LoadDmNguoiDung()
        {
            var lstDmNguoiDungEntity = new NguoiDungBusiness().GetNguoiDungList();
            var dmNguoiDungEntity = new NguoiDungEntity()
            {
                UserName = ConstantManagerClinic.sValueMember,
                TenNguoiDung = ConstantManagerClinic.sDisplayMember
            };
            lstDmNguoiDungEntity.Insert(0, dmNguoiDungEntity);

            lkNguoiDung.Properties.DataSource = lstDmNguoiDungEntity;
            lkNguoiDung.Properties.ValueMember = "UserName";
            lkNguoiDung.Properties.DisplayMember = "TenNguoiDung";
            lkNguoiDung.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadData()
        {
            lstCauHinhChiTietEntity = cauHinhChiTietBusiness.GetCauHinhChiTietList(MaCauHinh);

            lstDmPhongKhamEntity = dmPhongKhamBusiness.GetDmPhongKhamList();
            if (lstCauHinhChiTietEntity.Count > 0)
            {
                foreach (var dmPhongKhamEntity in lstDmPhongKhamEntity)
                {
                    lstGridCauHinhChiTietEntity.Add(new CauHinhChiTietEntity()
                    {
                        MaPhongKham = dmPhongKhamEntity.MaPhongKham,
                        TenPhongKham = dmPhongKhamEntity.TenPhongKham,
                        TrangThai = (lstCauHinhChiTietEntity.Where(n => n.MaPhongKham == dmPhongKhamEntity.MaPhongKham).ToList().Count > 0 ? true : false)
                    });
                }
            }
            else
            {
                foreach (var dmPhongKhamEntity in lstDmPhongKhamEntity)
                {
                    lstGridCauHinhChiTietEntity.Add(new CauHinhChiTietEntity()
                    {
                        MaPhongKham = dmPhongKhamEntity.MaPhongKham,
                        TenPhongKham = dmPhongKhamEntity.TenPhongKham,
                        TrangThai = false
                    });
                }
            }
            grdPhongLamViec.DataSource = lstGridCauHinhChiTietEntity;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkNguoiDung, "người dùng") == false) return;

            cauHinhEntity = new CauHinhEntity()
            {
                MaNguoiDung = lkNguoiDung.EditValue.ToString(),
                MaCauHinh = MaCauHinh
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                var lstNguoiDungChiTiet = cauHinhBusiness.GetCauHinhList();
                if (lstNguoiDungChiTiet.Count > 0 && lstNguoiDungChiTiet.Where(n => n.MaNguoiDung == lkNguoiDung.EditValue.ToString()).ToList().Count > 0)
                {
                    MessageBox.Show(ConstantManagerClinic.sTitleInformation, "Người dùng này đã được cấu hình!");
                    lkNguoiDung.Focus();
                    return;
                }
                MaCauHinh = cauHinhBusiness.InsertCauHinh(cauHinhEntity);
                if (MaCauHinh > 0)
                {
                    for (int i = 0; i < gvPhongLamViec.RowCount; i++)
                    {
                        var cauHinhChiTietEntity = (CauHinhChiTietEntity)gvPhongLamViec.GetRow(i);

                        if (cauHinhChiTietEntity.TrangThai == true)
                        {
                            cauHinhChiTietBusiness.InsertCauHinhChiTiet(new CauHinhChiTietEntity() { MaCauHinh = MaCauHinh, MaPhongKham = cauHinhChiTietEntity.MaPhongKham });
                        }
                    }
                }
                Validation.ClearControl(this);
                lkNguoiDung.Focus();
            }
            else
            {
                cauHinhChiTietBusiness.DeleteCauHinhChiTiet(MaCauHinh);
                cauHinhBusiness.EditCauHinh(cauHinhEntity);
                for (int i = 0; i < gvPhongLamViec.RowCount; i++)
                {
                    var cauHinhChiTietEntity = (CauHinhChiTietEntity)gvPhongLamViec.GetRow(i);

                    if (cauHinhChiTietEntity.TrangThai == true)
                    {
                        cauHinhChiTietBusiness.InsertCauHinhChiTiet(new CauHinhChiTietEntity() { MaCauHinh = MaCauHinh, MaPhongKham = cauHinhChiTietEntity.MaPhongKham });
                    }
                }
            }
            if (cauhinhEvent != null) { cauhinhEvent(sender, e); }
        }

        private void frmCauHinh_Load(object sender, EventArgs e)
        {

        }
    }
}